home *** CD-ROM | disk | FTP | other *** search
/ Club Amiga de Montreal - CAM / CAM_CD_1.iso / files / 493.lha / SmallIFFParseLibrary / sources / pasteclip.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-04-06  |  2.8 KB  |  101 lines

  1. /*
  2.  * Usage:       PasteClip
  3.  * Function:    Show the content of the clipboard if it is text.
  4.  * Description: This is an example of the standard way of using the 
  5.  *              iffparser in the most simple way, i.e. in the 
  6.  *              RAWSTEP-mode when you want to read data from the 
  7.  *              clipboard (and files).
  8.  *
  9.  * Written by:  Michael Jansson. 1990-12-08
  10.  */
  11.  
  12.  
  13. #include <libraries/iffparse.h>
  14. #include <proto/iffparse.h>
  15.  
  16. #define NAME        "Error in PasteClip: "
  17. #define ERR_LIB     NAME "Could not open iffparse.library!"
  18. #define ERR_MEM     NAME "Out of memory!"
  19. #define ERR_CLIP    NAME "Could not open the clipboard!"
  20. #define ERR_OPEN    NAME "Could not open primary clip in clipboard!"
  21. #define ERR_FAILED  NAME "Failed to read the clipboard (Error Code: %ld)!\n"
  22. #define LEVEL ". "
  23. #define DONE "Done!"
  24. #define SUCCESS 0L
  25.  
  26. struct Library *IFFParseBase;
  27.  
  28. void
  29. main(void)
  30. {
  31.     struct ContextNode *chunk = NULL;
  32.     struct IFFHandle *iff = NULL;
  33.     long error, size;
  34.     char *string;
  35.  
  36.     /* Open and initiate the usual stuff. */
  37.     if ((IFFParseBase=OpenLibrary("iffparse.library", 0L))==NULL) {
  38.         puts(ERR_LIB);
  39.         goto die;
  40.     }
  41.     if ((iff = AllocIFF())==NULL) {
  42.         puts(ERR_MEM);
  43.         goto die;
  44.     }
  45.     if ((iff->iff_Stream=(ULONG)OpenClipboard(0L))==0L) {
  46.         puts(ERR_CLIP);
  47.         goto die;
  48.     }
  49.     InitIFFasClip(iff);
  50.     if (OpenIFF(iff, IFFF_READ)) {
  51.         puts(ERR_OPEN);
  52.         goto die;
  53.     }
  54.  
  55.     /* Read the data. */
  56.     do {
  57.         switch(error=ParseIFF(iff, IFFPARSE_RAWSTEP)) {
  58.         case IFFERR_EOC:
  59.             break;
  60.         case IFFERR_EOF:
  61.             goto die;
  62.             break;
  63.         case SUCCESS:
  64.             chunk = CurrentChunk(iff);
  65.             /* The proper way to know when the propper chunk has been reached
  66.              * is to keep track of it with flags etc. There is a slight 
  67.              * possibility that there is a CHRS-FTXT in the clipboard that
  68.              * is not part of a FORM-FTXT chunk.
  69.              */
  70.             if (chunk->cn_ID=='CHRS' && chunk->cn_Type=='FTXT') {
  71.                 size = chunk->cn_Size;
  72.                 if ((string = (char *)AllocMem((long)size, 0L))==NULL) {
  73.                     puts(ERR_MEM);
  74.                     goto die;
  75.                 }
  76.                 ReadChunkBytes(iff, (APTR)string, (long)size);
  77.                 Write(Output(), string, size);
  78.                 FreeMem((APTR)string, (long)size);
  79.                 goto die;
  80.             }
  81.             break;
  82.         default:
  83.             printf(ERR_FAILED, error);
  84.             goto die;
  85.             break;
  86.         }
  87.     } while (TRUE);
  88.  
  89.     /* Terminate. */
  90. die:
  91.     if (iff) {
  92.         CloseIFF(iff);
  93.         if (iff->iff_Stream)
  94.            CloseClipboard((struct ClipboardHandle *)iff->iff_Stream);
  95.         FreeIFF(iff);
  96.     }
  97.     if (IFFParseBase)
  98.         CloseLibrary(IFFParseBase);
  99.     exit(0);
  100. }
  101.